package com.itheima.service.mongo.impl;

import com.itheima.domain.db.Comment;
import com.itheima.domain.db.Movement;
import com.itheima.service.mongo.CommentService;
import com.itheima.vo.PageBeanVo;
import org.apache.dubbo.config.annotation.DubboService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

/**
 * @author zsk
 * @create 2022-03-18-16:30
 */
@DubboService
public class CommentServiceImpl implements CommentService {

    @Autowired
    private MongoTemplate mongoTemplate;

    //保存喜欢、点赞、评论
    @Override
    public Integer saveComment(Comment comment) {

        //调用mango保存
        mongoTemplate.save(comment);

        //更新具体状态，如喜欢、评论、点赞
        switch (comment.getCommentType()) {
            case 1: {//状态为1代表点赞
                //查看具体动态详情
                Movement movement = mongoTemplate.findById(comment.getPublishId(), Movement.class);
                //代表点赞数+1
                movement.setLikeCount(movement.getLikeCount() + 1);
                //保存
                mongoTemplate.save(movement);
                //返回点赞数
                return movement.getLikeCount();
            }
            case 2: {//状态为2代表评论
                //查询动态详情
                Movement movement = mongoTemplate.findById(comment.getPublishId(), Movement.class);
                //设置评论数据
                movement.setCommentCount(movement.getCommentCount() + 1);
                //保存数据
                mongoTemplate.save(movement);

                //返回保存的评论数据
                return movement.getCommentCount();
            }
            case 3: {//状态为3代表喜欢
                //查询动态详情
                Movement movement = mongoTemplate.findById(comment.getPublishId(), Movement.class);

                //设置喜欢数量
                movement.setLoveCount(movement.getLoveCount() + 1);

                //保存
                mongoTemplate.save(movement);
                //返回保存后的喜欢数量
                return movement.getLoveCount();
            }
            case 4: {
            }
            case 5: {
            }
            case 6: {//状态6为评论点赞

            }
        }

        return null;
    }

    //删除喜欢、点赞、评论(参数，当前登录id,操作状态id,以及具体操作状态)
    @Override
    public Integer removeComment(Long userId, ObjectId publishId, Integer commentType) {

        //根据三个条件删除Comment
        Query query = new Query(
                Criteria.where("userId").is(userId)
                        .and("publishId").is(publishId)
                        .and("commentType").is(commentType)
        );
        //调用删除
        mongoTemplate.remove(query, Comment.class);

        //查看状态码，根据传递回来的状态码有选择的修改三个信息
        switch (commentType) {
            case 1: {//代表点赞
                //查询状态详情
                Movement movement = mongoTemplate.findById(publishId, Movement.class);
                //设置点赞数量
                movement.setLikeCount(movement.getLikeCount() - 1);

                //保存
                mongoTemplate.save(movement);

                //返回点赞数
                return movement.getLikeCount();
            }
            case 2: {//代表评论
                //查询动态详情
                Movement movement = mongoTemplate.findById(publishId, Movement.class);
                //设置评论数量
                movement.setCommentCount(movement.getCommentCount() - 1);

                //保存
                mongoTemplate.save(movement);

                //返回评论数
                return movement.getCommentCount();
            }
            case 3: {//代表喜欢

                //查询动态详情
                Movement movement = mongoTemplate.findById(publishId, Movement.class);
                //设置喜欢数量
                movement.setLoveCount(movement.getLoveCount() - 1);

                //保存
                mongoTemplate.save(movement);

                //返回喜欢数量
                return movement.getLoveCount();
            }
            case 4: {
            }
            case 5: {
            }
            case 6: {
            }
        }

        return null;
    }

    //展示动态评论
    @Override
    public PageBeanVo findCommentByPage(ObjectId publishId, Integer commentType, Integer pageNum, Integer pageSize) {

        //当前页数据
        Integer index = (pageNum - 1) * pageSize;

        //构建查询条件
        Query query = new Query(
                Criteria.where("publishId").is(publishId)
                        .and("commentType").is(2))
                .with(Sort.by(Sort.Order.desc("created")))//按照评论时间降序
                .skip(index).limit(pageSize);//展示第一页数据

        //查询
        List<Comment> commentList = mongoTemplate.find(query, Comment.class);

        //查询总记录数
        long count = mongoTemplate.count(query, Comment.class);

        //封装返回条件
        return new PageBeanVo(pageNum, pageSize, count, commentList);
    }

    //查看点赞、评论、喜欢详情
    @Override
    public PageBeanVo findUserCommentByPage(Long publishUserId, Integer commentType, Integer pageNum, Integer pageSize) {

        //构建条件
        Query query = new Query(
                Criteria.where("publishUserId").is(publishUserId)
                .and("commentType").is(commentType))//条件
                .with(Sort.by(Sort.Order.desc("created")))//按时间降序
                .skip((pageNum - 1) * pageSize).limit(pageSize);//分页

        //查询
        List<Comment> commentList = mongoTemplate.find(query, Comment.class);

        //返回
        return new PageBeanVo(pageNum,pageSize,0L,commentList);
    }

}
